/**
 * 
 */
package com.williamoneil.charts.client;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.williamoneil.charts.client.data.ChartElementsData;
import com.williamoneil.charts.client.data.ChartType;
import com.williamoneil.charts.client.util.ChartUtil;
import com.williamoneil.charts.client.widget.ChartWidget;
import com.williamoneil.charts.shared.ChartData;

/**
 * @author Gudipati
 *
 */
public class ChartPanel extends DockLayoutPanel {

	private ChartWidget chartWidget = null;
	
	//final Label debugLabel = new Label("");
	final Panel chartPanel = new SimplePanel();
	
	//private ChartBoundsRect chartBounds = null;
	
	public ChartPanel() {
		super(Unit.PX);
		
		/*
		super.setHeight("100%");
		super.setWidth("100%");
		 */
		chartPanel.setSize("100%", "100%");
		
		//final VerticalPanel vPanel = new VerticalPanel();

		//super.addNorth(debugLabel, 20);
		super.add(chartPanel);

		if(!Canvas.isSupported()) {
			final Label canvasNotSupportedLabel = new Label("This application required a browser that supports latest version of HTML standards.");
			chartPanel.add(canvasNotSupportedLabel);
			return;
		} else {
			final int height = chartPanel.getOffsetHeight();
			final int width = chartPanel.getOffsetWidth();
			
			chartWidget = new ChartWidget(height, width);
			chartPanel.add(chartWidget.getInternalPanel());
			
			// use deffered command to add to the panel
			Scheduler.get().scheduleDeferred(new ScheduledCommand() {
				@Override
				public void execute() {
					chartWidget.resetChartBounds();
				}
			});			
		}
	}
	
	public void drawPriceVolumeChart(final ChartData chartData, final ChartData benchmarkChartData) {
		try {
			if(chartWidget == null) {
				return;
			}
			
			final ChartElementsData chartElements = ChartUtil.getChartElements(chartData, benchmarkChartData, chartWidget.getChartBounds(), chartWidget.getChartLayer().isLinear(), chartWidget.getChartLayer().getTickSpacing());
			if(chartElements == null || chartElements.getTicks() == null) {
				return;
			}
	
			chartWidget.resetChartElements(chartElements);
			
			chartWidget.drawChart();
		} catch(Exception ex) {
			Charts.showError(ex);
		}
	}
	
	public void resized() {
		if(chartWidget == null) {
			return;
		}
		
		chartWidget.resetChartBounds();
	}
	
	public void changeTheme(final boolean isLight) {
		if(chartWidget == null) {
			return;
		}
		
		this.chartWidget.getChartLayer().changeTheme(isLight);
	}
	
	public void changeScale(final boolean isLinear) {
		if(chartWidget == null) {
			return;
		}
		
		this.chartWidget.getChartLayer().changeScale(isLinear);
	}
	
	public void changeChartType(final ChartType chartType) {
		if(chartWidget == null && chartType != null) {
			return;
		}
		
		this.chartWidget.getChartLayer().changeChartType(chartType);
	}
	
	public void changeTickSpacing(final int tickSpacing) {
		if(chartWidget == null) {
			return;
		}
		
		this.chartWidget.getChartLayer().changeTickSpacing(tickSpacing);
	}
}
